﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;

namespace DAO
{
    public class RuleObjectDAO
    {
        private RuleObjectDTO CreateRuleObjectDTOFromEntity(PhanQuyen pq)
        {
            try
            {
                using (BookStoreEntities ent = new BookStoreEntities())
                {
                    RuleObjectDTO rule = new RuleObjectDTO
                    {
                        GroupID = pq.LoaiNhanVienID,
                        RuleID = pq.QuyenID,
                        Status = pq.TinhTrang.HasValue ? pq.TinhTrang.Value : false
                    };

                    rule.RuleName = ent.Quyens.FirstOrDefault(r => r.QuyenID == rule.RuleID).TenQuyen;

                    return rule;
                }
            }
            catch
            {
                return null;
            }
        }

        public bool InsertRuleOfGroup(int ID)
        {
            try
            {
                using (BookStoreEntities ent = new BookStoreEntities())
                {
                    List<Quyen> listRule = ent.Quyens.ToList();
                    foreach (Quyen q in listRule)
                    {
                        PhanQuyen pq = new PhanQuyen
                        {
                            QuyenID = q.QuyenID,
                            LoaiNhanVienID = ID,TinhTrang = false
                        };

                        switch (q.QuyenID)
                        {
                            case (int)RuleObject.SellBook:
                            case (int)RuleObject.PayDebt:
                            case (int)RuleObject.Receiving:
                            case (int)RuleObject.BookList:
                            case (int)RuleObject.AuthorList:
                            case (int)RuleObject.CategoryList:
                            case (int)RuleObject.CustomerList:
                            case (int)RuleObject.EmployeeList:
                                pq.TinhTrang = true;
                                break;
                        }

                        ent.AddToPhanQuyens(pq);
                    }

                    ent.SaveChanges();

                    return true;
                }
            }
            catch
            {
                return false;
            }
        }

        public bool CheckPermission(int groupID, int RuleID)
        {
            try
            {
                using (BookStoreEntities ent = new BookStoreEntities())
                {
                    return ent.PhanQuyens.FirstOrDefault(r => r.LoaiNhanVienID == groupID && r.QuyenID == RuleID).TinhTrang.Value;
                }
            }
            catch
            {
                return false;
            }
        }

        public List<RuleObjectDTO> GetListRuleObject(int id)
        {
            try
            {
                using (BookStoreEntities ent = new BookStoreEntities())
                {
                    List<PhanQuyen> listPQ = ent.PhanQuyens.Where(p => p.LoaiNhanVienID == id).ToList();

                    List<RuleObjectDTO> listRule = new List<RuleObjectDTO>();

                    foreach (PhanQuyen pq in listPQ)
                    {
                        listRule.Add(CreateRuleObjectDTOFromEntity(pq));
                    }

                    return listRule;
                }
            }
            catch
            {
                return null;
            }
        }

        public bool UpdateRuleObject(List<RuleObjectDTO> listRule)
        {
            try
            {
                using (BookStoreEntities ent = new BookStoreEntities())
                {
                    foreach (RuleObjectDTO rule in listRule)
                    {
                        PhanQuyen pq = ent.PhanQuyens.FirstOrDefault(p => p.LoaiNhanVienID == rule.GroupID && p.QuyenID == rule.RuleID);
                        pq.TinhTrang = rule.Status;
                    }

                    ent.SaveChanges();
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }
    }
}
